{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Difference Between Ones and Zeros in Row and Column"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: onesMinusZeros"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #行和列中一和零的差值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的&nbsp;<code>m x n</code>&nbsp;二进制矩阵&nbsp;<code>grid</code>&nbsp;。</p>\n",
    "\n",
    "<p>我们按照如下过程，定义一个下标从 <strong>0</strong>&nbsp;开始的&nbsp;<code>m x n</code>&nbsp;差值矩阵&nbsp;<code>diff</code>&nbsp;：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>令第&nbsp;<code>i</code>&nbsp;行一的数目为&nbsp;<code>onesRow<sub>i</sub></code>&nbsp;。</li>\n",
    "\t<li>令第&nbsp;<code>j</code>&nbsp;列一的数目为&nbsp;<code>onesCol<sub>j</sub></code><sub>&nbsp;</sub>。</li>\n",
    "\t<li>令第&nbsp;<code>i</code>&nbsp;行零的数目为&nbsp;<code>zerosRow<sub>i</sub></code>&nbsp;。</li>\n",
    "\t<li>令第&nbsp;<code>j</code>&nbsp;列零的数目为&nbsp;<code>zerosCol<sub>j</sub></code>&nbsp;。</li>\n",
    "\t<li><code>diff[i][j] = onesRow<sub>i</sub> + onesCol<sub>j</sub> - zerosRow<sub>i</sub> - zerosCol<sub>j</sub></code></li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回差值矩阵<em>&nbsp;</em><code>diff</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img src=\"https://assets.leetcode.com/uploads/2022/11/06/image-20221106171729-5.png\" style=\"width: 400px; height: 208px;\"></p>\n",
    "\n",
    "<pre><b>输入：</b>grid = [[0,1,1],[1,0,1],[0,0,1]]\n",
    "<b>输出：</b>[[0,0,4],[0,0,4],[-2,-2,2]]\n",
    "<b>解释：</b>\n",
    "- diff[0][0] = <code>onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0 \n",
    "- diff[0][1] = <code>onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0 \n",
    "- diff[0][2] = <code>onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4 \n",
    "- diff[1][0] = <code>onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub></code> = 2 + 1 - 1 - 2 = 0 \n",
    "- diff[1][1] = <code>onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub></code> = 2 + 1 - 1 - 2 = 0 \n",
    "- diff[1][2] = <code>onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub></code> = 2 + 3 - 1 - 0 = 4 \n",
    "- diff[2][0] = <code>onesRow<sub>2</sub> + onesCol<sub>0</sub> - zerosRow<sub>2</sub> - zerosCol<sub>0</sub></code> = 1 + 1 - 2 - 2 = -2\n",
    "- diff[2][1] = <code>onesRow<sub>2</sub> + onesCol<sub>1</sub> - zerosRow<sub>2</sub> - zerosCol<sub>1</sub></code> = 1 + 1 - 2 - 2 = -2\n",
    "- diff[2][2] = <code>onesRow<sub>2</sub> + onesCol<sub>2</sub> - zerosRow<sub>2</sub> - zerosCol<sub>2</sub></code> = 1 + 3 - 2 - 0 = 2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img src=\"https://assets.leetcode.com/uploads/2022/11/06/image-20221106171747-6.png\" style=\"width: 358px; height: 150px;\"></p>\n",
    "\n",
    "<pre><b>输入：</b>grid = [[1,1,1],[1,1,1]]\n",
    "<b>输出：</b>[[5,5,5],[5,5,5]]\n",
    "<strong>解释：</strong>\n",
    "- diff[0][0] = onesRow<sub>0</sub> + onesCol<sub>0</sub> - zerosRow<sub>0</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5\n",
    "- diff[0][1] = onesRow<sub>0</sub> + onesCol<sub>1</sub> - zerosRow<sub>0</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5\n",
    "- diff[0][2] = onesRow<sub>0</sub> + onesCol<sub>2</sub> - zerosRow<sub>0</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5\n",
    "- diff[1][0] = onesRow<sub>1</sub> + onesCol<sub>0</sub> - zerosRow<sub>1</sub> - zerosCol<sub>0</sub> = 3 + 2 - 0 - 0 = 5\n",
    "- diff[1][1] = onesRow<sub>1</sub> + onesCol<sub>1</sub> - zerosRow<sub>1</sub> - zerosCol<sub>1</sub> = 3 + 2 - 0 - 0 = 5\n",
    "- diff[1][2] = onesRow<sub>1</sub> + onesCol<sub>2</sub> - zerosRow<sub>1</sub> - zerosCol<sub>2</sub> = 3 + 2 - 0 - 0 = 5\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == grid.length</code></li>\n",
    "\t<li><code>n == grid[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>grid[i][j]</code>&nbsp;要么是&nbsp;<code>0</code>&nbsp;，要么是&nbsp;<code>1</code> 。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [difference-between-ones-and-zeros-in-row-and-column](https://leetcode.cn/problems/difference-between-ones-and-zeros-in-row-and-column/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [difference-between-ones-and-zeros-in-row-and-column](https://leetcode.cn/problems/difference-between-ones-and-zeros-in-row-and-column/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[0,1,1],[1,0,1],[0,0,1]]', '[[1,1,1],[1,1,1]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        c,r=[0 for i in range(n)],[0 for i in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    c[j]+=1\n",
    "                    r[i]+=1\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j]=2*r[i]+2*c[j]-m-n\n",
    "                \n",
    "        return grid\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        r = [0] * len(grid)\n",
    "        c = [0] * len(grid[0])\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                r[i] += x * 2 - 1\n",
    "                c[j] += x * 2 - 1\n",
    "        for i, x in enumerate(r):\n",
    "            for j, y in enumerate(c):\n",
    "                grid[i][j] = x + y\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        onerow = [0] * m\n",
    "        onecol = [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]:\n",
    "                    onerow[i] += 1\n",
    "                    onecol[j] += 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = 2 * onerow[i] + 2 * onecol[j] - m - n\n",
    "        return grid       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0])\n",
    "        diff = grid[:]\n",
    "        ones_row = [0 for i in range(rows)]\n",
    "        ones_col = [0 for i in range(cols)]\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if grid[i][j] == 1:\n",
    "                    ones_row[i] += 1\n",
    "                    ones_col[j] += 1\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                diff[i][j] = ones_row[i] * 2 - rows + ones_col[j] * 2 - cols\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        row = [0] * len(grid)\n",
    "        column = [0] * len(grid[0])\n",
    "\n",
    "        for i, r in enumerate(grid):\n",
    "            for j , c in enumerate(r):\n",
    "                row[i] += c * 2 -1\n",
    "                column[j] += c * 2 - 1\n",
    "\n",
    "        for i, r in enumerate(row):\n",
    "            for j, c in enumerate(column):\n",
    "                grid[i][j] = r + c\n",
    "        return grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        x = len(grid)\n",
    "        y = len(grid[0])\n",
    "        row_one = [0] * x\n",
    "        col_one = [0] * y\n",
    "        row_zero = [0] * x\n",
    "        col_zero = [0] * y\n",
    "        for i in range(x):\n",
    "            for j in range(y):\n",
    "                if grid[i][j] == 1:\n",
    "                    row_one[i] += 1\n",
    "                    col_one[j] += 1\n",
    "                elif grid[i][j] == 0:\n",
    "                    row_zero[i] += 1\n",
    "                    col_zero[j] += 1\n",
    "        for i in range(x):\n",
    "            for j in range(y):\n",
    "                grid[i][j] = row_one[i] + col_one[j] - row_zero[i] - col_zero[j]\n",
    "        return grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        行 = [0]*m\n",
    "        列 = [0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                行[i] += grid[i][j]*2-1\n",
    "                列[j] += grid[i][j]*2-1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = 行[i] + 列[j]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        row, col = len(grid), len(grid[0])\n",
    "        row1, row0, col1, col0 = [], [], [], []\n",
    "        for i in range(row):\n",
    "            temp = sum(grid[i])\n",
    "            row1.append(temp)\n",
    "            row0.append(col - temp)\n",
    "        for i in range(col):\n",
    "            temp = sum(grid[j][i] for j in range(row))\n",
    "            col1.append(temp)\n",
    "            col0.append(row - temp)\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                grid[i][j] = row1[i] + col1[j] - row0[i] - col0[j]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        # 计算每行每列的1值\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        li1 = []\n",
    "        li2 = []\n",
    "        for g in grid:\n",
    "            li1.append(sum(g))\n",
    "        print(li1)\n",
    "\n",
    "        for i in range(n):\n",
    "            temp = 0\n",
    "            for j in range(m):\n",
    "                temp += grid[j][i]\n",
    "            li2.append(temp)\n",
    "        print(li2)\n",
    "\n",
    "        # 第i行1的个数=li1[i],0的个数为m-li1[i]\n",
    "        # 第j列1的个数=li2[j],0的个数为n-li2[j]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = li1[i] + li2[j] - (m-li1[i]) - (n-li2[j])\n",
    "        print(grid)\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        or_i = [row.count(1) for row in grid]\n",
    "        zr_i = [row.count(0) for row in grid]\n",
    "\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0]) \n",
    "\n",
    "        oc_j = []\n",
    "        zc_j = []\n",
    "        for i in range(cols):\n",
    "            col = [grid[j][i] for j in range(rows)]\n",
    "            oc_j.append(col.count(1))\n",
    "            zc_j.append(col.count(0))\n",
    "        \n",
    "        # print(or_i, zr_i, oc_j, zc_j)\n",
    "        res = grid.copy()\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                res[i][j] = or_i[i] + oc_j[j] - zr_i[i] - zc_j[j]\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        # m, n = len(grid), len(grid[0])\n",
    "        # row_1, row_0 = [], []\n",
    "        # for i in range(m):\n",
    "        #     row1, row0 = sum(grid[i]), m - sum(grid[i])\n",
    "        #     row_1.append(row1) \n",
    "        #     row_0.append(row0)\n",
    "        # col_1, col_0 = [], []\n",
    "        # for j in range(n):\n",
    "        #     tmp = []\n",
    "        #     for i in range(m):\n",
    "        #         tmp.append(grid[i][j])\n",
    "        #     col1, col0 = sum(tmp), n - sum(tmp)\n",
    "        #     col_1.append(col1)\n",
    "        #     col_0.append(col0)\n",
    "\n",
    "        # diff = [[0]*n for _ in range(m)]\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         diff[i][j] = row_1[i] + col_1[j] - row_0[i] - col_0[j]\n",
    "        # return diff\n",
    "\n",
    "\n",
    "        n = len(grid)\n",
    "        m = len(grid[0])\n",
    "        row = [0] * n\n",
    "        col = [0] * m\n",
    "\n",
    "        for i in range(n):\n",
    "            row[i] = 2 * sum(grid[i]) - n\n",
    "        \n",
    "        for j in range(m):\n",
    "            tmp = 0\n",
    "            for i in range(n):\n",
    "                tmp += grid[i][j]\n",
    "            col[j] = 2 * tmp - m\n",
    "        \n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                grid[i][j] = row[i] + col[j]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        col = [[0, 0] for _ in range(m)]\n",
    "        raw = [[0, 0] for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 0:\n",
    "                    col[i][0] += 1\n",
    "                    raw[j][0] += 1\n",
    "                else:\n",
    "                    col[i][1] += 1\n",
    "                    raw[j][1] += 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = raw[j][1] + col[i][1] - raw[j][0] - col[i][0]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        diff = [([0] * n) for _ in range(m)]\n",
    "        line_set = [0] * n\n",
    "        for i, line in enumerate(grid):\n",
    "            hang = (sum(line) << 1) - n\n",
    "            for j, k in enumerate(line):\n",
    "                diff[i][j] = hang\n",
    "                line_set[j] += (k << 1) - 1\n",
    "                \n",
    "        for i in range(len(diff)):\n",
    "            for j in range(len(diff[i])):\n",
    "                diff[i][j] += line_set[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        row = [sum(l) << 1 for l in grid]\n",
    "        col = [sum(l) << 1 for l in zip(*grid)]\n",
    "        mn = len(grid) + len(grid[0])\n",
    "        for i, x in enumerate(row):\n",
    "            for j, y in enumerate(col):\n",
    "                grid[i][j] = x + y - mn\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        onesRow, zerosRow = [0] * m, [0] * m\n",
    "        for i in range(m):\n",
    "            tmp = grid[i].count(1)\n",
    "            onesRow[i] = tmp\n",
    "            zerosRow[i] = n - tmp\n",
    "        onesCol, zerosCol = [0] * n, [0] * n\n",
    "        trans_grid = list(zip(*grid))\n",
    "        for j in range(n):\n",
    "            tmp = trans_grid[j].count(1)\n",
    "            onesCol[j] = tmp\n",
    "            zerosCol[j] = m - tmp\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = onesRow[i] + onesCol[j] - zerosRow[i] - zerosCol[j]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        row_one = [sum(grid[i]) for i in range(m)]\n",
    "        col_one = [sum([grid[i][j] for i in range(m)]) for j in range(n)]\n",
    "        row_zero = [m-sum(grid[i]) for i in range(m)]\n",
    "        col_zero = [n - sum([grid[i][j] for i in range(m)]) for j in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = row_one[i]+col_one[j]-row_zero[i]-col_zero[j]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        ans = [[0] * n for _ in grid]\n",
    "\n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "\n",
    "        for i, r in enumerate(grid):\n",
    "            for j, val in enumerate(r):\n",
    "                row[i] += val\n",
    "                row[i] -= val ^ 1\n",
    "                col[j] += val\n",
    "                col[j] -= val ^ 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = row[i] + col[j]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        n, m = len(grid), len(grid[0])\n",
    "        onesRow = [0] * n\n",
    "        onesCol = [0] * m\n",
    "        zerosRow = [0] * n\n",
    "        zerosCol = [0] * m\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j] == 1:\n",
    "                    onesRow[i] += 1\n",
    "                    onesCol[j] += 1\n",
    "                else:\n",
    "                    zerosRow[i] += 1\n",
    "                    zerosCol[j] += 1\n",
    "        \n",
    "        res = [[0] * m for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                res[i][j] = onesRow[i] + onesCol[j] - zerosRow[i] - zerosCol[j]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0])\n",
    "        R = [0 for _ in range(rows)]\n",
    "        C = [0 for _ in range(cols)]\n",
    "        for i in range(rows):\n",
    "            R[i] = grid[i].count(1)\n",
    "        for i in range(cols):\n",
    "            cnt = 0\n",
    "            for j in range(rows):\n",
    "                if grid[j][i]==1:cnt+=1\n",
    "            C[i] = cnt\n",
    "        diff = [[0]*cols for _ in range(rows)]\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                diff[i][j] = R[i]+C[j]-(rows-C[j])-(cols-R[i])\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        diff = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        onesrow, zerosrow = [0] * m, [0] * m\n",
    "        onescol, zeroscol = [0] * n, [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    onesrow[i] += 1\n",
    "                    onescol[j] += 1\n",
    "                else:\n",
    "                    zerosrow[i] += 1\n",
    "                    zeroscol[j] += 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = onesrow[i] + onescol[j] - zerosrow[i] - zeroscol[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        grid_2 = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        onesRow = [0 for _ in range(m)]\n",
    "        zerosRow = [0 for _ in range(m)]\n",
    "        onesCol = [0 for _ in range(n)]\n",
    "        zerosCol = [0 for _ in range(n)]\n",
    "\n",
    "        for i in range(0,m):\n",
    "            for j in range(0,n):\n",
    "                if(grid[i][j] == 0):\n",
    "                    zerosRow[i] += 1\n",
    "                    zerosCol[j] +=1\n",
    "                else:\n",
    "                    onesCol[j] += 1\n",
    "                    onesRow[i] += 1\n",
    "        \n",
    "        for i in range(0,m):\n",
    "            for j in range(0,n):\n",
    "                grid_2[i][j] = onesRow[i] + onesCol[j] - zerosRow[i]-zerosCol[j]\n",
    "        return grid_2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        # 1的数量 - 0的数量\n",
    "        # sum(1)-sum(0)\n",
    "        # sum(0) = len -sum(1)\n",
    "        # 2sum(1)-len + 2sum(1)-len2\n",
    "        # 转置\n",
    "        trans_grid = list(zip(*grid))\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        ans = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        # 优化sum计算次数\n",
    "        sum_row = [sum(i) for i in grid]\n",
    "        sum_col = [sum(i) for i in trans_grid]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = 2 * sum_row[i] - m + 2 * sum_col[j] - n\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        sum_row = [sum(i) for i in grid]\n",
    "        sum_col = [sum(i) for i in zip(*grid)]\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        ans = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = sum_row[i] + sum_col[j] - (m-sum_row[i] + n-sum_col[j])\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        n, m = len(grid), len(grid[0])\n",
    "        r0, r1 = [0] * n, [0] * n\n",
    "        c0, c1 = [0] * m, [0] * m\n",
    "        for i in range(n):\n",
    "            r0[i] = grid[i].count(0)\n",
    "            r1[i] = m - r0[i]\n",
    "        for j in range(m):\n",
    "            c1[j] = sum(grid[i][j] for i in range(n))\n",
    "            c0[j] = n - c1[j]\n",
    "        ans = [[0] * m for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                ans[i][j] = r1[i] + c1[j] - r0[i] - c0[j]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "          diff=[([0]*len(grid[0])) for i in range(len(grid))]\n",
    "          k=[[a[i] for a in grid]for i in range(len(grid[0]))]\n",
    "          a=[sum(k[i]) for i in range(len(k))]\n",
    "          b=[sum(grid[i]) for i in range(len(grid))]\n",
    "          for i in range(len(grid)):\n",
    "              for j in range(len(grid[0])):\n",
    "                  diff[i][j]=2*b[i]+2*a[j]-len(grid[i])-len(k[j])\n",
    "          return diff   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        rows_count = [0] * m\n",
    "        cols_count = [0] * n\n",
    "        ans = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    rows_count[i] += 1\n",
    "                    cols_count[j] += 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = 2*rows_count[i]-n + 2*cols_count[j]-m\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        '''思路：\n",
    "        1、对grid进行切片，切出第i行和第j列————该步不需要\n",
    "        2、统计第i行中的0的个数，和第j列中0的个数，用总长度相减，得到i 和j中1的个数：\n",
    "        如何提取二维数组的列a = [i[1] for i in grid]\n",
    "        3、i和j中1的个数相加，减去i和j中0的个数，作为diff[i][j]的值，for循环填充：\n",
    "        如何创建一个二维数组diff = [0] * len(grid)\n",
    "        for i in range(0, len(grid)):\n",
    "             diff[i] = [0] * len(grid[i])\n",
    "        4、时间复杂度超了，应该是O（n^3），优化为O（n^2）需要把计算列中1的个数的for循环优化掉\n",
    "        '''\n",
    "\n",
    "        # import numpy as np\n",
    "        diff = [0] * len(grid)\n",
    "        onesRowi = [0] * len(grid)\n",
    "        onesColj = [0] * len(grid[0])\n",
    "        gridColj = [0] * len(grid[0])\n",
    "\n",
    "        for i in range(0, len(grid)):\n",
    "            # '''计算行中1的数量，不使用count函数'''\n",
    "            onesRowi[i] = sum(grid[i][:])\n",
    "            diff[i] = [0] * len(grid[i])\n",
    "        for j in range(0, len(grid[0])):\n",
    "            # '''通过numpy取列，使用count内置函数'''   \n",
    "            # gridColj[j] = list(np.array(grid)[:, j])\n",
    "            gridColj[j] = [m[j] for m in grid]\n",
    "        for j in range(0, len(gridColj)):\n",
    "            onesColj[j] = sum(gridColj[j])\n",
    "        for i in range(0, len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                diff[i][j] = 2 * (onesRowi[i] + onesColj[j]) - len(grid) - len(grid[i])\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        \n",
    "        rows = [0] * m\n",
    "        cols = [0] * n\n",
    "        for i in range(m):\n",
    "            rows[i] = sum(grid[i])\n",
    "        \n",
    "        for j in range(n):\n",
    "            cnt = 0\n",
    "            for i in range(m):\n",
    "                cnt += grid[i][j]\n",
    "            cols[j] = cnt\n",
    "        \n",
    "        diff = [[0] * n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = 2 * rows[i] - n + 2 * cols[j] - m\n",
    "                \n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "      m, n = len(grid), len(grid[0])\n",
    "      row_ones, col_ones = [0] * m, [0] * n\n",
    "\n",
    "      for i in range(m):\n",
    "        for j in range(n):\n",
    "          if grid[i][j] == 1:\n",
    "            row_ones[i] += 1\n",
    "            col_ones[j] += 1\n",
    "\n",
    "      diff = [[0] * n for _ in range(m)]\n",
    "      for i in range(m):\n",
    "        for j in range(n):\n",
    "          diff[i][j] = row_ones[i] + col_ones[j] - (m - row_ones[i]) - (n - col_ones[j])\n",
    "      return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid):\n",
    "        self.rows = len(grid)\n",
    "        self.cols = len(grid[0])\n",
    "        ones_row = [0] * self.rows\n",
    "        ones_col = [0] * self.cols\n",
    "        zero_row = [0] * self.rows\n",
    "        zero_col = [0] * self.cols\n",
    "\n",
    "        for i in range(self.rows):\n",
    "            ones_row[i] = grid[i].count(1)\n",
    "            zero_row[i] = grid[i].count(0)\n",
    "        for j in range(self.cols):\n",
    "            for k in range(self.rows):\n",
    "                if grid[k][j] == 1:\n",
    "                    ones_col[j] += 1\n",
    "                else:\n",
    "                    zero_col[j] += 1\n",
    "        # print('ones_row',ones_row)\n",
    "        # print('ones_col',ones_col)\n",
    "        # print('zero_row',zero_row)\n",
    "        # print('zero_col',zero_col)\n",
    "        self.res = [[0] * self.cols for _ in range(self.rows)]\n",
    "        self.make_list(ones_row, zero_row, ones_col, zero_col)\n",
    "        return self.res\n",
    "\n",
    "    def make_list(self,ones_row, zero_row, ones_col, zero_col):\n",
    "        for i in range(self.rows):\n",
    "            for j in range(self.cols):\n",
    "                self.res[i][j] = ones_row[i] + ones_col[j] - zero_row[i] - zero_col[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        row=[0]*m\n",
    "        col=[0]*n\n",
    "        diff=[[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            row[i]=sum(grid[i])\n",
    "        for j in range(n):\n",
    "            col[j]=sum(grid[k][j] for k in range(m))\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j]=2*row[i]+2*col[j]-m-n\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        a,b=[0]*m,[0]*n\n",
    "        aa,bb=[0]*m,[0]*n\n",
    "        for i,row in enumerate(grid):\n",
    "            a[i]=sum((1 if x==1 else 0) for x in row)\n",
    "        for j in range(n):\n",
    "            b[j]=sum((1 if x==1 else 0) for x in [r[j] for r in grid])\n",
    "        #print(a)\n",
    "        #print(b)\n",
    "        aa=[n-a[i] for i in range(m)]\n",
    "        bb=[m-b[j] for j in range(n)]\n",
    "        diff=[[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j]=a[i]+b[j]-(aa[i]+bb[j])\n",
    "        return diff\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        row = [sum(g) for g in grid]\n",
    "        col = [sum(g) for g in zip(*grid)]\n",
    "        ans = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = 2*row[i]+2*col[j]-m-n\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        a = [-n] * m\n",
    "        b = [-m] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 0: continue\n",
    "                a[i] += 2\n",
    "                b[j] += 2\n",
    "        return [[a[i]+b[j] for j in range(n)] for i in range(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        mm,nn=[0]*m,[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    mm[i]+=1\n",
    "                    nn[j]+=1\n",
    "                else:\n",
    "                    mm[i]-=1\n",
    "                    nn[j]-=1\n",
    "        #print(mm,nn)\n",
    "        diff=[[0 for _ in range(n)]for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j]=mm[i]+nn[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0]) # m行n列\n",
    "        diff = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        # 先遍历每一行\n",
    "        for i in range(m):\n",
    "            one_row = sum(grid[i])\n",
    "            zero_row = n - one_row\n",
    "            for j in range(n):\n",
    "                diff[i][j] += (one_row-zero_row)\n",
    "        # 遍历每一列\n",
    "        for j in range(n):\n",
    "            one_column = sum([row[j] for row in grid])\n",
    "            zero_column = m - one_column\n",
    "            for i in range(m):\n",
    "                diff[i][j] += (one_column-zero_column)\n",
    "        return diff\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        n,m=len(grid),len(grid[0])\n",
    "        a=[sum(map(lambda x:grid[i][x] ,range(m))) for i in range(n)]\n",
    "        b=[sum(map(lambda x:grid[x][i] ,range(n))) for i in range(m)]\n",
    "        return [list(map(lambda x:2*x+2*v-m-n,b)) for v in a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        row = []\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        for r in range(m):\n",
    "            num_1 = 0\n",
    "            for c in range(n):\n",
    "                if grid[r][c]==1:num_1+=1\n",
    "            row.append(num_1)\n",
    "        col =[]\n",
    "        for c in range(n):\n",
    "            num_1 = 0\n",
    "            for r in range(m):\n",
    "                if grid[r][c]==1:num_1+=1\n",
    "            col.append(num_1)\n",
    "            \n",
    "        _grid =[]\n",
    "        for r in row:\n",
    "            _row = []\n",
    "            for c in col:\n",
    "                v = r+r+c+c-m-n\n",
    "                _row.append(v)\n",
    "            _grid.append(_row)\n",
    "        return _grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        one_row = []\n",
    "        one_col = []\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        for row in grid:\n",
    "            one_row.append(sum(row))\n",
    "        \n",
    "        for col in zip(*grid):\n",
    "            one_col.append(sum(col))\n",
    "        \n",
    "        ans = []\n",
    "        for i in range(m):\n",
    "            tmp = []\n",
    "            for j in range(n):\n",
    "                tmp.append(2 * one_row[i] + 2 * one_col[j] - m - n)\n",
    "            ans.append(tmp)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        onesRow,onesCol,zerosRow,zerosCol = [],[],[],[]\n",
    "        for i in range(len(grid)):\n",
    "            onesTmp,zerosTmp = 0,0\n",
    "            for j in range(len(grid[i])):\n",
    "                if grid[i][j] == 1:\n",
    "                    onesTmp += 1\n",
    "                else:\n",
    "                    zerosTmp += 1\n",
    "            onesRow.append(onesTmp)\n",
    "            zerosRow.append(zerosTmp)\n",
    "        for j in range(len(grid[0])):\n",
    "            onesTmp,zerosTmp = 0,0\n",
    "            for i in range(len(grid)):\n",
    "                if grid[i][j] == 1:\n",
    "                    onesTmp += 1\n",
    "                else:\n",
    "                    zerosTmp += 1\n",
    "            onesCol.append(onesTmp)\n",
    "            zerosCol.append(zerosTmp)\n",
    "        res = []\n",
    "        for i in range(len(grid)):\n",
    "            tmp = []\n",
    "            for j in range(len(grid[i])):\n",
    "                tmp.append(onesRow[i] + onesCol[j] - zerosRow[i] - zerosCol[j])\n",
    "            res.append(tmp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        cnt = [[[0 for _ in range(4)] for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            cnt_1 = 0\n",
    "            cnt_0 = 0\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    cnt_1 += 1\n",
    "                elif grid[i][j] == 0:\n",
    "                    cnt_0 += 1\n",
    "            for j in range(n):\n",
    "                cnt[i][j][0] = cnt_1\n",
    "                cnt[i][j][1] = cnt_0\n",
    "        for i in range(n):\n",
    "            cnt_1 = 0\n",
    "            cnt_0 = 0\n",
    "            for j in range(m):\n",
    "                if grid[j][i] == 1:\n",
    "                    cnt_1 += 1\n",
    "                elif grid[j][i] == 0:\n",
    "                    cnt_0 += 1\n",
    "            for j in range(m):\n",
    "                cnt[j][i][2] = cnt_1\n",
    "                cnt[j][i][3] = cnt_0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                grid[i][j] = cnt[i][j][0] + cnt[i][j][2] - cnt[i][j][1] -cnt[i][j][3]\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        row_1, row_0 = [], []\n",
    "        for i in range(m):\n",
    "            row1, row0 = sum(grid[i]), m - sum(grid[i])\n",
    "            row_1.append(row1) \n",
    "            row_0.append(row0)\n",
    "        col_1, col_0 = [], []\n",
    "        for j in range(n):\n",
    "            tmp = []\n",
    "            for i in range(m):\n",
    "                tmp.append(grid[i][j])\n",
    "            col1, col0 = sum(tmp), n - sum(tmp)\n",
    "            col_1.append(col1)\n",
    "            col_0.append(col0)\n",
    "\n",
    "        diff = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = row_1[i] + col_1[j] - row_0[i] - col_0[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        diff = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        grid_col = [list(i) for i in zip(*grid)]\n",
    "        # 预处理\n",
    "        one_row, zero_row = [], []\n",
    "        one_col, zero_col = [], []\n",
    "        for i in range(0, m):\n",
    "            one_row.append(grid[i].count(1))\n",
    "            zero_row.append(grid[i].count(0))\n",
    "        for i in range(0, n):\n",
    "            one_col.append(grid_col[i].count(1))\n",
    "            zero_col.append(grid_col[i].count(0))\n",
    "        \n",
    "        for i in range(0, m):\n",
    "            for j in range(0, n):\n",
    "                diff[i][j] = one_row[i] + one_col[j] - zero_row[i] - zero_col[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        M, N = len(grid), len(grid[0])\n",
    "        rowCnt = []\n",
    "        for row in grid:\n",
    "            rowCnt.append([row.count(0), row.count(1)])\n",
    "        \n",
    "        colCnt = []\n",
    "        for col in zip(*grid):\n",
    "            colCnt.append([col.count(0), col.count(1)])\n",
    "        \n",
    "        diff = [[0] * N for i in range(M)]\n",
    "\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                diff[i][j] = rowCnt[i][1] + colCnt[j][1] - rowCnt[i][0] - colCnt[j][0]\n",
    "        \n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        lim = [sum([1 if grid[j][i] == 1 else -1 for i in range(n)]) for j in range(m)]\n",
    "        lin = [sum([1 if grid[j][i] == 1 else -1 for j in range(m)]) for i in range(n)]\n",
    "        diff = deepcopy(grid)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = lim[i] + lin[j]\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        r = [0] * len(grid)\n",
    "        c = [0] * len(grid[0])\n",
    "        for i,row in enumerate(grid):\n",
    "            for j,x in enumerate(row):\n",
    "                r[i] += x * 2 - 1\n",
    "                c[j] += x * 2 - 1\n",
    "        for i,x in enumerate(r):\n",
    "            for j,y in enumerate(c):\n",
    "                grid[i][j] = x + y\n",
    "        return grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        n,m = len(grid),len(grid[0])\n",
    "        grid_t = [list(i) for i in zip(*grid)]\n",
    "        row = [[m-sum(grid[i]),sum(grid[i])] for i in range(n)]\n",
    "        col = [[n-sum(grid_t[i]),sum(grid_t[i])] for i in range(m)]\n",
    "        \n",
    "        ans = [[0]*m for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                ans[i][j] = row[i][1] + col[j][1] - row[i][0] - col[j][0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        n, m = len(grid), len(grid[0])\n",
    "        onerow, onecol, zerorow, zerocol = defaultdict(int), defaultdict(int), defaultdict(int), defaultdict(int)\n",
    "        res = [[0 for _ in range(m)] for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j]:\n",
    "                    onerow[i] += 1\n",
    "                    onecol[j] += 1\n",
    "                # else:\n",
    "                #     zerorow[i] += 1\n",
    "                #     zerocol[j] += 1\n",
    "        \n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                res[i][j] = onerow[i] + onecol[j] - (m - onerow[i]) - (n - onecol[j])\n",
    "                #onesRowi + onesColj - zerosRowi - zerosColj\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        diff = []\n",
    "\n",
    "        row_data = []\n",
    "        col_data = []\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            onesrow = 0\n",
    "            zerosrow = 0\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j] == 0:\n",
    "                    zerosrow += 1\n",
    "                else:\n",
    "                    onesrow += 1\n",
    "            row_data.append((zerosrow, onesrow))\n",
    "\n",
    "        for i in range(len(grid[0])):\n",
    "            onescol = 0\n",
    "            zeroscol = 0\n",
    "            for j in range(len(grid)):\n",
    "                if grid[j][i] == 0:\n",
    "                    zeroscol += 1\n",
    "                else:\n",
    "                    onescol += 1\n",
    "            col_data.append((zeroscol, onescol))\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            ans = []\n",
    "            for j in range(len(grid[0])):\n",
    "                ans.append(row_data[i][1] + col_data[j][1] - row_data[i][0] - col_data[j][0])\n",
    "            diff.append(ans)\n",
    "\n",
    "        return diff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        row = [0] * m\n",
    "        n = len(grid[0])\n",
    "        column = [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                row[i] += grid[i][j] * 2 - 1\n",
    "                column[j] += grid[i][j] * 2 - 1\n",
    "        ans = [[[0] for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = row[i] + column[j]\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        onesRow, oneCol, zerosRow, zerosCol = [0] * m, [0] * n, [0] * m, [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    onesRow[i] += 1\n",
    "                    oneCol[j] += 1\n",
    "                if grid[i][j] == 0:\n",
    "                    zerosRow[i] += 1\n",
    "                    zerosCol[j] += 1\n",
    "        ans = [[[0] for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = onesRow[i] + oneCol[j] - zerosRow[i] - zerosCol[j]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        ones_cnt_row = defaultdict(int)\n",
    "        ones_cnt_col = defaultdict(int)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    ones_cnt_row[i] += 1\n",
    "                    ones_cnt_col[j] += 1\n",
    "        res = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                res[i][j] = 2 * ones_cnt_row[i] - n + 2 * ones_cnt_col[j] - m\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        diff = [[0] * n for _ in range(m)]\n",
    "\n",
    "        row, col = {}, {}\n",
    "        for i in range(m):\n",
    "            row[i] = sum(grid[i])\n",
    "\n",
    "        for j in range(n):\n",
    "            res = 0\n",
    "            for i in range(m):\n",
    "                res += grid[i][j]\n",
    "            col[j] = res\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = row[i] + col[j] - (n - row[i]) - (m - col[j])\n",
    "\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        rowDiff = {}\n",
    "        colDiff = {}\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        for i in range(m):\n",
    "            row_sum = 0\n",
    "            for j in range(n):\n",
    "                row_sum += grid[i][j]\n",
    "            rowDiff[i] = 2*row_sum - n\n",
    "        for j in range(n):\n",
    "            col_sum = 0\n",
    "            for i in range(m):\n",
    "                col_sum += grid[i][j]\n",
    "            colDiff[j] = 2*col_sum - m\n",
    "        result = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                result[i][j] = rowDiff[i] + colDiff[j]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        diff=[[0]*n for _ in range(m)]\n",
    "        rowdic = { x:sum(grid[x])*2-m   for x in range(m)}\n",
    "        newmatrix = list(zip(*grid))\n",
    "        coldic = { y:sum(newmatrix[y])*2 -n   for y in range(n)}\n",
    "        #print(diff)\n",
    "        #print(rowdic)\n",
    "        #print(coldic)\n",
    "\n",
    "        for x in range(m):\n",
    "            for y in range(n):\n",
    "                diff[x][y] = rowdic[x]+coldic[y]\n",
    "        \n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        row2one = {}\n",
    "        col2one = {}\n",
    "        for i, row in enumerate(grid):\n",
    "            row2one[i] = sum(row)\n",
    "        for j, col in enumerate(zip(*grid)):\n",
    "            col2one[j] = sum(col)\n",
    "\n",
    "        diff = [[0 for i in range(n)] for j in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = row2one[i] + col2one[j] - (n - row2one[i]) - (m - col2one[j])\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        \n",
    "        m,n = len(grid),len(grid[0])\n",
    "        onesRow = dict()\n",
    "        zeroRow = dict()\n",
    "        onesCol = dict()\n",
    "        zeroCol = dict()\n",
    "        for i in range(m):\n",
    "            onesRow[i] = sum(grid[i])\n",
    "            zeroRow[i] = n-sum(grid[i])\n",
    "        for j in range(n):\n",
    "            onesCol[j] = sum([grid[x][j] for x in range(m)])\n",
    "            zeroCol[j] = m-onesCol[j]\n",
    "        \n",
    "        diff = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = onesRow[i]+onesCol[j]-zeroRow[i]-zeroCol[j]\n",
    "        return diff\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        ans = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        \n",
    "        rows = [[row.count(0), row.count(1)] for row in grid]\n",
    "        cols = [[col.count(0), col.count(1)] for col in zip(*grid)]\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = rows[i][1] + cols[j][1]  - rows[i][0] - cols[j][0] \n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        res = grid.copy()\n",
    "        grid_ = np.array(grid)\n",
    "        \n",
    "        row_one_list = []\n",
    "        row_zero_list = []\n",
    "        col_one_list = []\n",
    "        col_zero_list = []\n",
    "        for i in range(len(grid)):\n",
    "            onesRowi = np.sum(grid_[i, :])\n",
    "            row_one_list.append(onesRowi)\n",
    "            row_zero_list.append(n - onesRowi)\n",
    "        for j in range(len(grid[0])):\n",
    "            onesColj = np.sum(grid_[:, j])\n",
    "            col_one_list.append(onesColj)\n",
    "            col_zero_list.append(m - onesColj)\n",
    "        \n",
    "        for i in range(len(row_one_list)):\n",
    "            for j in range(len(col_one_list)):\n",
    "                res[i][j] = int(row_one_list[i] + col_one_list[j] - row_zero_list[i] - col_zero_list[j])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        matrix = np.array(grid).astype(np.int)\n",
    "        row_ones = matrix.sum(axis=1)\n",
    "        col_ones = matrix.sum(axis=0)\n",
    "        result = np.zeros((m, n)).astype(np.int)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                result[i][j] = 2 * row_ones[i] + 2 * col_ones[j] - m - n\n",
    "        return result.tolist()\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        res = grid.copy()\n",
    "        grid_ = np.array(grid)\n",
    "        \n",
    "        row_one_list = []\n",
    "        row_zero_list = []\n",
    "        col_one_list = []\n",
    "        col_zero_list = []\n",
    "        for i in range(len(grid)):\n",
    "            onesRowi = np.sum(grid_[i, :])\n",
    "            row_one_list.append(onesRowi)\n",
    "            row_zero_list.append(n - onesRowi)\n",
    "        for j in range(len(grid[0])):\n",
    "            onesColj = np.sum(grid_[:, j])\n",
    "            col_one_list.append(onesColj)\n",
    "            col_zero_list.append(m - onesColj)\n",
    "        \n",
    "        for i in range(len(row_one_list)):\n",
    "            for j in range(len(col_one_list)):\n",
    "                res[i][j] = int(row_one_list[i] + col_one_list[j] - row_zero_list[i] - col_zero_list[j])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        grid = np.asarray(grid)\n",
    "        N, M = grid.shape\n",
    "        rows = np.sum(grid, axis=0)\n",
    "        cols = np.sum(grid, axis=1)\n",
    "        mat = copy.deepcopy(grid)\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                mat[i][j] = rows[j] + cols[i] - (N - rows[j]) - (M - cols[i])\n",
    "        return mat.tolist()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        x=[]\n",
    "        for i in grid:\n",
    "            l=collections.Counter(i)\n",
    "            x.append(l[1])\n",
    "        grid2=list(map(list, zip(*grid)))\n",
    "        y=[]\n",
    "        for i in grid2:\n",
    "            l=collections.Counter(i)\n",
    "            y.append(l[1])\n",
    "        diff = [[0] * n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j]=2*int(x[i])-int(m)+2*int(y[j])-int(n)\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        grid = np.array(grid)\n",
    "        \n",
    "        r = []\n",
    "        rm = []\n",
    "        rn = []\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        for i in range(m):\n",
    "            a = 0\n",
    "            for ii in range(n):\n",
    "                if grid[i,ii] == 0:\n",
    "                    a -= 1\n",
    "                else:\n",
    "                    a += 1\n",
    "            rm.append(a)\n",
    "        for j in range(n):\n",
    "            b = 0\n",
    "            for jj in range(m):\n",
    "                if grid[jj,j] == 0:\n",
    "                    b -= 1\n",
    "                else:\n",
    "                    b += 1\n",
    "            rn.append(b)\n",
    "        #print(rm,rn)\n",
    "        for x in rm:\n",
    "            r1 = []\n",
    "            for y in rn:\n",
    "                r1.append(x+y)\n",
    "            r.append(r1)\n",
    "        #print(r)\n",
    "        # for i in range(m):\n",
    "        #     r1 = []\n",
    "            # a = 0\n",
    "#             for ii in range(n):\n",
    "#                 if grid[i,ii] == 0:\n",
    "#                     a -= 1\n",
    "#                 else:\n",
    "#                     a += 1\n",
    "                \n",
    "#             for j in range(n):\n",
    "#                 b = a\n",
    "#                 for jj in range(m):\n",
    "#                     if grid[jj,j] == 0:\n",
    "#                         b -= 1\n",
    "#                     else:\n",
    "#                         b += 1\n",
    "        #         r1.append(b)                \n",
    "        #     r.append(r1)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        rowOne = [0]*len(grid)\n",
    "        colOne = [0]* len(grid[0])\n",
    "        # print(rowOne)\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                # print(grid[i][j])\n",
    "                rowOne[i] += int(grid[i][j])\n",
    "                colOne[j] += int(grid[i][j])\n",
    "        diff = [[0 for _ in range(len(grid[0]))] for _ in range(len(grid))]\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                diff[i][j] = 2*rowOne[i] + 2* colOne[j] - len(grid) - len(grid[0])\n",
    "        return diff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        '''思路：\n",
    "        1、对grid进行切片，切出第i行和第j列————该步不需要\n",
    "        2、统计第i行中的0的个数，和第j列中0的个数，用总长度相减，得到i 和j中1的个数：\n",
    "        如何提取二维数组的列a = [i[1] for i in grid]\n",
    "        3、i和j中1的个数相加，减去i和j中0的个数，作为diff[i][j]的值，for循环填充：\n",
    "        如何创建一个二维数组diff = [0] * len(grid)\n",
    "        for i in range(0, len(grid)):\n",
    "             diff[i] = [0] * len(grid[i])\n",
    "        4、时间复杂度超了，应该是O（n^3），优化为O（n^2）需要把计算列中1的个数的for循环优化掉\n",
    "        '''\n",
    "\n",
    "        import numpy as np\n",
    "        diff = [0] * len(grid)\n",
    "        onesRowi = [0] * len(grid)\n",
    "        onesColj = [0] * len(grid[0])\n",
    "        gridColj = [0] * len(grid[0])\n",
    "\n",
    "        for i in range(0, len(grid)):\n",
    "            # '''计算行中1的数量，不使用count函数'''\n",
    "            onesRowi[i] = sum(grid[i][:])\n",
    "            diff[i] = [0] * len(grid[i])\n",
    "        for j in range(0, len(grid[0])):\n",
    "            # '''通过numpy取列，使用count内置函数'''   \n",
    "            # gridColj[j] = list(np.array(grid)[:, j])\n",
    "            gridColj[j] = [m[j] for m in grid]\n",
    "        for j in range(0, len(gridColj)):\n",
    "            onesColj[j] = sum(gridColj[j])\n",
    "            print(onesColj[j])\n",
    "        for i in range(0, len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                diff[i][j] = 2 * (onesRowi[i] + onesColj[j]) - len(grid) - len(grid[i])\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        grid = np.array(grid)\n",
    "        row = grid.sum(axis=1)\n",
    "        col = grid.sum(axis=0)\n",
    "        res = [[0] * n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                res[i][j] = int((row[i] + col[j])) * 2 - m - n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "       \n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        \n",
    "        onesrow = [0]* m\n",
    "        onescol = [0]*n \n",
    "        #对行进行求和       \n",
    "        #对列进行求和\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    onesrow[i] +=1\n",
    "                    onescol[j] +=1 \n",
    "                else:\n",
    "                    onesrow[i] -=1\n",
    "                    onescol[j] -=1\n",
    "\n",
    "        diff = [[0]*n for i in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = onesrow[i] + onescol[j]\n",
    "        return diff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        onesRowi = dict()\n",
    "        onesColj = dict()\n",
    "\n",
    "        zerosRowi = dict()\n",
    "        zerosColj = dict()\n",
    "\n",
    "\n",
    "        diff = [[0] * n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            onesRowi[i] = sum(grid[i])\n",
    "            zerosRowi[i] = m - sum(grid[i])\n",
    "\n",
    "        for i in range(n):\n",
    "            cur = 0\n",
    "            for j in range(m):\n",
    "                cur += grid[j][i]\n",
    "            zerosColj[i] = n - cur\n",
    "            onesColj[i] = cur\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                diff[i][j] = onesRowi[i] + onesColj[j] - zerosRowi[i] - zerosColj[j]\n",
    "\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        array = np.array(grid)\n",
    "        arrays = [np.sum(array == x, axis = y, keepdims=True) for x in [1, 0] for y in [1, 0]]\n",
    "        res = arrays[0] + arrays[1] - arrays[2] - arrays[3]\n",
    "        return res.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        hang_ling = collections.defaultdict(int)\n",
    "        hang_yi = collections.defaultdict(int)\n",
    "        lie_ling = collections.defaultdict(int)\n",
    "        lie_yi = collections.defaultdict(int)\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==0:\n",
    "                    hang_ling[i] += 1\n",
    "                    lie_ling[j] +=1\n",
    "                else:\n",
    "                    hang_yi[i]+=1\n",
    "                    lie_yi[j]+=1\n",
    "        dp = [[0]*n for i in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                dp[i][j] = hang_yi[i]+lie_yi[j]-hang_ling[i]-lie_ling[j]\n",
    "        return dp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        g=np.transpose(grid)\n",
    "        onerow=[]\n",
    "        onecol=[]\n",
    "        for i in grid:\n",
    "            onerow.append(sum(i))\n",
    "        for i in g:\n",
    "            onecol.append(sum(i))\n",
    "        t=[]\n",
    "        for i in range(m):\n",
    "            tmp=[]\n",
    "            for j in range(n):\n",
    "                tmp.append(int(onerow[i]*2-m+onecol[j]*2-n))\n",
    "            t.append(tmp)\n",
    "        return t"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
